package cc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.GetUserList;
import com.bootdo.common.utils.HttpUtil;
import com.bootdo.common.wx.WeiXinUtil;
import com.bootdo.system.domain.Media_id;
import com.bootdo.system.domain.SendPojo;

public class cnmd {
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
		  String accessToken =WeiXinUtil.getOauth2AccessToken2("wxcb74c2a7fb1fd161", "3ba771e951e1e0e917b7dcd777a25e06");
		  String sendMsg = sendMsg(accessToken,"Wr8SVx7Dgzgll4I2-xlgj-GW9ZG5t6YpFTUsmH3D6Mc");
		  System.out.println(sendMsg);
	}
	
	public static String sendMsg(String token,String id) throws ClientProtocolException, IOException {
		//先获取用户openid列表
		String url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token="+token;
		List userList =GetUserList.getUserList(token);
		String openid = userList.toString();
		System.out.println(openid);
		Media_id m = new Media_id();
		m.setMedia_id(id);
		JSONObject json1 = (JSONObject) JSONObject.toJSON(m);
		String str1 = json1.toString();
		System.out.println(str1);
		//封装请求参数
		SendPojo sp = new SendPojo();
		sp.setTouser(userList);
		sp.setMpnews(str1);
		sp.setMsgtype("mpnews");
		sp.setSend_ignore_reprint(0);
		JSONObject json = (JSONObject) JSONObject.toJSON(sp);
		String str = json.toString();
		str= str.replace("\\", "");  
		str= str.replace("\"{", "{");
		str= str.replace("}\"", "}");
		System.out.println(str);
		return HttpUtil.httpPost(url, null, str);
	}

}
